package com.sensePlusVR.activity.follow;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sensePlusVR.animation.MyAnimation;
import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.bean.GameItem;
import com.sensePlusVR.main.R;
import com.sensePlusVR.setting.bluetooth.BlueScannerActivity;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.GameController;
import com.sensePlusVR.utils.GameDownload;
import com.sensePlusVR.utils.JumpGameTest;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MyPageJump;
import com.sensePlusVR.utils.MySharedPreferences;
import com.sensePlusVR.widget.dialog.BaseDialog;

public class VideoPlayDialog extends BaseDialog implements View.OnClickListener{
	private static final String TAG = VideoPlayDialog.class.getSimpleName();
	private Context context;
	
	private List<GameItem> selectGameList;

	private VRApplication vrApplication;

	private LinearLayout root_play_linear;
	private LinearLayout play_linear;
	private ImageView play_common_iv;
	private ImageView play_vr_iv;
	private ImageView play_close_iv;
	
	public VideoPlayDialog(Context context,List<GameItem> selectGameList) {
		super(context,R.style.FullScreen_Dialog_Half_Tran);
		this.context = context;
		this.selectGameList = selectGameList;
		init();
	}
	
	public VideoPlayDialog(Context context, int theme) {
		super(context, theme);
		this.context = context;
		init();
	}
	
	private void init() {}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video_play);
		beforeView();
		initView();
	}
	
	public void beforeView() {
		vrApplication = (VRApplication)((Activity)context).getApplication();
	}

	public void initView() {
		root_play_linear = (LinearLayout) findViewById(R.id.root_play_linear);
		play_linear = (LinearLayout) findViewById(R.id.play_linear);
		play_common_iv = (ImageView) findViewById(R.id.play_common_iv);
		play_vr_iv = (ImageView) findViewById(R.id.play_vr_iv);
		play_close_iv = (ImageView) findViewById(R.id.play_close_iv);
		
		play_linear.setLayoutAnimation(MyAnimation.getAnimationControllerVisible(Constant.ANIMATION_PLAY_BUTTON_DURATION));
		play_close_iv.startAnimation(MyAnimation.animationVisible(Constant.ANIMATION_PLAY_BUTTON_DURATION));

		root_play_linear.setOnClickListener(this);
		play_common_iv.setOnClickListener(this);
		play_vr_iv.setOnClickListener(this);
		play_close_iv.setOnClickListener(this);
		
		GameItem gameItem = selectGameList.get(0);
		if(gameItem != null){
			boolean isVROnly = GameItem.isVROnly(gameItem);
			boolean is2DOnly = GameItem.is2DOnly(gameItem);
			if(isVROnly){
				play_common_iv.setImageResource(R.drawable.play_common_d);
				play_common_iv.setClickable(false);
			}
			if(is2DOnly){
				play_vr_iv.setImageResource(R.drawable.play_vr_d);
				play_vr_iv.setClickable(false);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.root_play_linear:
			MyLog.d(TAG, "root_play_linear");
			break;
		case R.id.play_vr_iv:
			MyLog.d(TAG, "play_vr_iv");
			gameStart(Constant.GAME_MODE_VR);
			break;
		case R.id.play_common_iv:
			MyLog.d(TAG, "play_common_iv");
			gameStart(Constant.GAME_MODE_2D);
			break;
		case R.id.play_close_iv:
			Log.d(TAG, "play_close_iv...");
			break;
		}
		dismiss();
	}
	
	private void gameStart(int gameMode) {
		MySharedPreferences.save(context, "shouldFinish", false);
		MySharedPreferences.save(context, Constant.SP_BLUE_IS_FROM_SETTING, false);

		List<String> glist = new ArrayList<String>();
		
		int realCount = 0;
		int unRealCount = 0;
		
		if(selectGameList == null || selectGameList.size() <= 0)return;
		
			GameItem gameItem = selectGameList.get(0);
			
			if(gameItem.getType().equals(Constant.TYPE_DEMO_REAL)||gameItem.getType().equals(Constant.TYPE_REAL)){
				
				realCount++;
				
			}else{
				unRealCount++;
			}
				
			glist.add(GameDownload.getGameConfig(gameItem));
		
		if(realCount!=0&&unRealCount!=0){
			
			return;
		}
		
		boolean isRealGame = realCount>unRealCount;
		
		
		boolean isConnectedVR = vrApplication.isBTConnected();
		if (isConnectedVR) {
			boolean isFromSetting = MySharedPreferences.getBoolean(context,
					Constant.SP_BLUE_IS_FROM_SETTING);
			MyLog.d(TAG, "glist : " + glist.size());
			if (!isFromSetting) {
				
				if (Constant.nativeBundleSwitch) {

					if (isRealGame) {

						JumpGameTest.jumpRealActivity(context,
								gameMode == Constant.GAME_MODE_VR,gameItem);

					} else {

						JumpGameTest.jumpVRActivity(context,
								gameMode == Constant.GAME_MODE_VR);
					}

				} else {
					GameController.GameStart(context, gameMode, isRealGame,glist,gameItem);
				}
			}
		} else {
			vrApplication.toBTDisconnect();

			if (!Constant.nativeBundleSwitch) {

						// 没有连接上则先set数据到GameController中，然后在bluescan中在GameStartOne不用传数据了.
						GameController.GameSetList(gameMode, isRealGame, glist,gameItem);

					} else {

						GameController.GameSetList(gameMode, isRealGame, null,gameItem);

					}

					MyPageJump.pageJump(context, BlueScannerActivity.class);
		}
	}

}
